import React, { Component } from "react"
import storage from "good-storage"
import { Provider } from "mobx-react"
import Routes from "routes/index"
import userAuthFun from "./creativeMatch/api/userAuth"
import stores from "./creativeMatch/stores/index"
import ApiModel from "./creativeMatch/api/apiModel/apiModel"
import { getQuery, removeQuery } from "./creativeMatch/utils/common"

class App extends Component {
  async componentWillMount() {
    if (stores.UserStore.token && stores.UserStore.token !== "") {
      let userMsg = await ApiModel.getUserMsg()
      if (userMsg && userMsg.code === 0) {
        userAuthFun.checkUserStatus(userMsg)
      } else {
        storage.set("token", "")
      }
    }

    // 单纯的维护后端信息
    let wxFlag = this.isWeiXin()
    if (wxFlag) {
      if (stores.UserStore.token && stores.UserStore.token !== "") {
        let userMsg = await ApiModel.getUserMsg()
        console.log(userMsg)
        if (userMsg && userMsg.code === 0) {
          userAuthFun.checkUserStatus(userMsg)
        } else {
          storage.set("token", "")
          let newUrl = removeQuery("code", window.location.href)
          window.location.href = newUrl
        }
      } else {
        await userAuthFun.checkUserAuth()
      }
    }
  }

  async componentDidMount() {
    // console.log('检测头部连接有没有别人的分享标志')
    // await userAuthFun.checkUserAuth()
  }

  isWeiXin() {
    var ua = window.navigator.userAgent.toLowerCase()
    console.log(ua)
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
      return true
    } else {
      return false
    }
  }

  // 重置字体

  render() {
    return (
      <div className="app">
        <Provider {...stores}>
          <Routes />
        </Provider>
      </div>
    )
  }
}

export default App
